//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var title;
var limit = 4; //每页条数
var page = 1; //页数
var isOver = false; //是否加载完
var self;

//下拉刷新

mui("body").on("tap", "#search", function() {
	mui.init({
		pullRefresh: {
			container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
			up: {
				auto: false, // 可选,默认false.自动上拉加载一次
				contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
				contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
				callback: pullupRefresh //加载数据方法 
			}
		}
	})
	isOver = false;
	$(".product_details").html("")

	title = $("#search_text").val();
	if(!title) {
		mui.toast("请输入想购买的商品");
	} else {

		page = 1;
		mui.init();
		itemPartitionList(isOver);
	}

});
//所属区域商品集合
function itemPartitionList(isOver) {
	mui.ajax(goods_url + '/goods/itemList', {
		data: {
			"limit": limit,
			"page": page,
			"sign": "string",
			"title": title
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				var p = data.total / limit; //总页数
				if(page >= p) { //当前的页数
					isOver = true;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
				}
				if(isOver == false) {
					page++;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true); //停止正在加载
					mui('#refreshContainer').pullRefresh().enablePullupToRefresh(); //显示上拉加载文字
					mui.toast("为您更新了商品" + data.resp.length + "条信息");
				}
				var html = '';
				if(data.resp != "") {
					for(var i = 0; i < data.resp.length; i++) {
						html += '<li class="mui-col-xs-6 mui-col-sm-6 product_color product_jump_con" commodity_id="' + data.resp[i].id + '">';
						html += '<div class="mui-col-xs-6 mui-col-sm-6 product_img">';
						html += '<img width="100%" class="lazy" data-original="' + data.resp[i].url + data.resp[i].image + '"/>';
						html += '</div>';
						html += '<p class="product_name">' + data.resp[i].title + '</p>';
						html += '<p class="product_num">￥' + data.resp[i].price + '</p>';
						html += '<div class="dealer">';
						if(data.resp[i].sales_volume == null) {
							html += '<p>销量 0</p>';
						} else {
							html += '<p>销量 ' + data.resp[i].sales_volume + '</p>';
						}
						if(data.resp[i].commercial == 1) {
							html += '<span class="dealer_icon">自营</span>';
						} else if(data.resp[i].commercial == 2) {
							html += '<span class="dealer_icon">商家</span>';
						}
						html += '</div>';
						html += '</li>';
					}
					$('.product_details').append(html);
					$("img.lazy").lazyload({
						effect: "fadeIn",
						placeholder: "../../images/as.jpg",
						container: $(".product_details"),
						failurelimit: 10
					});
				} else {
					mui.toast("没有该商品");
				}
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});

}

function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh(false);
		setTimeout(function() {
			itemPartitionList(); //ajax 
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。
		}, 500);
	}, 500);
}

//商品详情
mui("body").on("tap", ".product_jump_con", function() {
	var commodity_id = $(this).attr('commodity_id');
	mui.openWindow({
		url: "commodity_details.html",
		extras: {
			commodity_id: commodity_id
		},
		createNew: true
	})
});

//判断ios
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if(isiOS) {
	$(".mui-scroll-wrapper").css("padding-top", "50px")
}